<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 
include_once("./mysql/database_info.php");
include_once("./mysql/common.php");
include_once("./common.php");

function printCategoryBlock($categories, $categoryTags){
	
	$countCat = count($categories);
	
	$countTags = count($categoryTags);
	
	if ($countCat != $countTags){
		
		printf("!!!ERROR!!! Array counts do not match.");
		
		return false;
	}
	
	//makes sure we have even rows of three.
	while (($countCat%3) != 0){
		
		$countCat++;
		
		$countTags++;
	}
		
	for($i=0;$i<$countCat;$i++){

		if ($categories[$i]){
		
			$result = mysql_query("SELECT * FROM `needs` WHERE `category`='".$categoryTags[$i]."' && `pending`='0' ORDER BY `postDate` DESC LIMIT 0,3");
			
			if (mysql_num_rows($result) != 0){
			
				printf("<div class='needsBlock'>\n");
				
				printf("<h4 class='needsBlockTitle'>%s</h4>\n", $categories[$i]);
				
				while ($recentPosts = mysql_fetch_array($result)){
				
					if (strlen($recentPosts['title']) >= 50){
						
						$postTitle = substr($recentPosts['title'], 0, 47)."...";
					}
					else {
						
						$postTitle = $recentPosts['title'];
					}
					$postDate = date("D, M jS, Y", strtotime($recentPosts['postDate']));
	
					printf("<a href='/needsPosted.php?id=%s'><div class='postBlock'><h6>%s</h6>%s</div></a>\n",$recentPosts['id'], $postTitle , $postDate);
				}
				
				printf("<a href='/needsCategory.php?cat=%s'><div class='postBlock'><h6>View All Posts in Category.</h6></div></a>\n", $categoryTags[$i]);
				
				printf("</div>\n");
			
			}
			else{
				
				printf("<div class='needsBlock'>\n");
				
				printf("<h4 class='needsBlockTitle'>%s</h4>\n", $categories[$i]);
				
				printf("<br><p>Praise God! There are currently no needs in this category!</p>");
				
				printf("</div>\n");
			}
		}
		else {
			
			printf("<div class='needsBlock'></div>\n");			
		}
	}
}

$title = "Needs";
$meta_key='needs, clothes, clothing, auto, cars, rides, furniture, housing, place to stay, food, need, needs, help, services, labor, laborers, house hold items';
$meta_desc="A forum to post your needs here at SHCC, and get help quickly.";
print_html_head($title ,$meta_key,$meta_desc);
print_java_head(null);

connect_select_test("needs");

begin_body();
page_title_no_content($title);

if ($_REQUEST['needsLogin'] == 1){
	
	$categories = array("Furniture", "Clothes", "House Hold Items", "Services/Labor", "Food", "Automotive", "Housing", "Miscellaneous");
	
	$categoryTags = array("furniture", "clothing", "hhItems", "services", "food", "auto", "housing", "misc");
	
	printf("<div class='content'>\n");
	
	printf("<button style='float:right;' onclick='refreshPage(\"/needsPost.php\")'>Post a Need</button>\n");
	
	printf("<p>Welcome to the South Hill Calvary needs board!<br><br>Please take some time to browse the needs within the church. If you feel like you can help someone out, go ahead and click on the need and you will be taken to a page where you can send them an email.</p>");
	
	printf("</div><br>\n");
	
	printf("<div class='holder'>\n");
	
	printCategoryBlock($categories, $categoryTags);
	
	mysql_close();
	
}
else{
	
	printf("<div class='content'>\n");
	
	printNeedsLogin("/needs.php", null);
	
	printf("</div>\n");
}

end_page_title();
end_body();
printf("</div>\n");

?>